#!/bin/bash
source /home/shell/util.sh

serversPrint
read -p "远端服务器地址：" remote_host
read -p "远端服务器用户名：" remote_username
read -p "远端服务器密码：" remote_host_passwd
if [[ -z "${remote_host}" ]] || [[ -z "${remote_username}" ]] || [[ -z "${remote_host_passwd}" ]]; then
    printFail "服务端信息有误，程序已退出！"
    exit;
fi
if [ ! -d ~/.ssh/ ]; then
    mkdir ~/.ssh/&&chmod 700 ~/.ssh/
fi
if [ ! -f "~/.ssh/authorized_keys" ]; then
    touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
fi
if [ ! -f "~/.ssh/known_hosts" ]; then
    touch ~/.ssh/known_hosts
fi
ssh-keyscan $remote_host >> ~/.ssh/known_hosts
if [ ! -f "~/.ssh/id_rsa.pub" ]; then
    ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""
fi
sshpass -p ${remote_host_passwd} ssh-copy-id -i ~/.ssh/id_rsa.pub ${remote_username}@${remote_host}